#pragma once
#include <iostream>
#include <vector>
#include <ctemplate/template.h>
#include "oj_model.hpp"

namespace ns_view
{
    using namespace ns_model;

    const std::string template_path = "./template_html/";
    class View
    {
    public:
        View(){};
        ~View(){};
        void AllExpandHtml(std::vector<Question>& all, std::string* html)
        {
            //1.形成路径
            std::string src_path = template_path + "all_questions.html";
            //2.形成数据字典
            ctemplate::TemplateDictionary root("all_questions");
            for(const auto& q : all)
            {
                ctemplate::TemplateDictionary* sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number",q.number);
                sub->SetValue("title",q.title);
                sub->SetValue("dif_level",q.dif_level);
            }

            //3.获取被渲染的html的对象
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_path,ctemplate::DO_NOT_STRIP);
            //4.完成渲染
            tpl->Expand(html,&root);
            
        }

        void OneExpandHtml(Question& q, std::string* html)
        {
            //1.形成路径
            std::string src_path = template_path + "one_question.html";
            //2.形成数据字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number",q.number);
            root.SetValue("title",q.title);
            root.SetValue("dif_level",q.dif_level);
            root.SetValue("desc",q.desc);
            root.SetValue("header",q.header);
            //3.获取被渲染的html对象
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_path,ctemplate::DO_NOT_STRIP);
            //4.完成渲染
            tpl->Expand(html,&root);

        }

    };
}